<?php

namespace Beerus\Modules\Extend\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\tools\ImgUrl as toolImgUrl;
use Hhzbeads\Img\ImgUpload;

/**
 * description
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     zhouyang  <zhouyang@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class ImgController extends Controller
{
    public function upload()
    {
        $file = $_FILES["file_data"]["tmp_name"];

        if (!class_exists('CURLFile')) {
            $data['file'] = '@' . $file;
        } else {
            $imgSize = getimagesize($file);
            $data['file'] = new \CURLFile($file, $imgSize['mime'], 'file');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_URL, config('domain.upload_url') . "/Biz-imageUpload");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode == 200) {
            $result = json_decode($response, true);
            return response()->json([
                "code" => 1,
                "data" => ["picId" => $result["saveName"]]
            ]);
        } else {
            return response()->json([
                "code" => 0
            ]);
        }
    }

    public function uploadToSso()
    {
        $file = $_FILES["file_data"]["tmp_name"];
        $picId = ImgUpload::upload(ImgUpload::APPTYPE_BIZ, $file);
        if (is_array($picId) && !empty($picId)) {
            return response()->json([
                "code" => 1,
                "data" => [
                    "picId" => $picId["ori_pic_id"],
                    'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $picId['ori_pic_id'], toolImgUrl::IMAGE_SIZE_SQ_PHONE),
                    'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $picId['ori_pic_id'], toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                ],
            ]);
        } else {
            return response()->json([
                "code" => 0
            ]);
        }
    }


    /*
     * 此方法仅限上传原图使用
     * **/
    public function uploadToSsoOri()
    {
        $file = $_FILES["file_data"]["tmp_name"];
        $picId = ImgUpload::upload(ImgUpload::APPTYPE_OP, $file);

        if (is_array($picId) && !empty($picId)) {
            return response()->json([
                "code" => 1,
                "data" => [
                    "picId" => $picId["ori_pic_id"],
                    'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_OP, $picId['ori_pic_id'], toolImgUrl::IMAGE_SIZE_OP_ORI),
                    'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_OP, $picId['ori_pic_id'], toolImgUrl::IMAGE_SIZE_OP_ORI)
                ],
            ]);
        } else {
            return response()->json([
                "code" => 0
            ]);
        }
    }

    public function imgDelete()
    {
        return response()->json([
            "code" => 0
        ]);
    }
}
